package cc.vv.btpartyjob.partymanager.view

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import cc.vv.baselibrary.util.CommonUtil
import cc.vv.btpartyjob.partymanager.R
import cc.vv.lkbasecomponent.util.LKScreenUtil
import cc.vv.lkimagecomponent2.LKImage
import cc.vv.lkimagecomponent2.lib.mode.ScaleMode
import com.dawning.utiming.task.bean.FileUrlObj
import com.dawning.utiming.task.inter.SelectedOrDeleteFileInterface
import java.util.*

/**
 * 图片上传自定义view
 * Created by Chen.qi on 2018/10/9.
 */
class QTSelectedFileShowView : LinearLayout {
    /**
     * 固定占位数据
     */
    private val zwStrData = "zhaWeiShuJu"
    /**
     * 上下文
     */
    private var mContext: Context? = null

    /**
     * 父布局
     */
    private var llSelectedAll: LinearLayout? = null

    /**
     * 删除事件监听
     */
    private var deleteImageOnClick = DeleteImgageOnClick()

    /**
     * 查看大图
     */
    private var largerImageOnClick = LargerImageOnClick()

    private var deletedSelectedImageInter: SelectedOrDeleteFileInterface? = null

    fun getDeletedSelectedImageInter(): SelectedOrDeleteFileInterface? {
        return deletedSelectedImageInter
    }

    fun setDeletedSelectedImageInter(deletedSelectedImageInter: SelectedOrDeleteFileInterface) {
        this.deletedSelectedImageInter = deletedSelectedImageInter
    }


    /**
     * 数据集
     */
    private var dataList = ArrayList<FileUrlObj>()

    constructor(context: Context) : this(context, null) {
        mContext = context
    }

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) {
        mContext = context
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        mContext = context
        val inflateView = View.inflate(context, R.layout.layout_selectd_file_view, null)
        llSelectedAll = inflateView.findViewById(R.id.ll_asj_selectedAll)
        this.addView(inflateView)
    }


    /**
     * 填充选项数据
     *
     * @param optionList 数据集
     * @param itemSize   每行有几个选项
     */
    fun addAllSelectedFileView(optionList: ArrayList<FileUrlObj>?, itemSize: Int) {
        clearAllJobOptionView()
        if (null == optionList) {
            return
        }
        //清除
        dataList.clear()
        dataList.addAll(optionList)
        val length = dataList.size
        val sum = length / itemSize
        val add = if (length % itemSize == 0) 0 else 1
        val totalLinLayout = sum + add

        val viewWight = (LKScreenUtil.getScreenWidth() - LKScreenUtil.dp2px(38f)) / itemSize
        val layoutParams1 = LayoutParams(viewWight, viewWight + LKScreenUtil.dp2px(15f))

        for (j in 0 until totalLinLayout) {
            val honLinearLayout = LinearLayout(context)
            honLinearLayout.setPadding(0, LKScreenUtil.dp2px(10f), 0, 0)
            val iNumber = j * itemSize
            var count = 0
            for (i in iNumber until dataList.size) {
                count++
                if (count == itemSize + 1) {
                    break
                }
                val view = LayoutInflater.from(mContext).inflate(R.layout.layout_selected_file_item, null)
                view.layoutParams = layoutParams1
                val img = view.findViewById<ImageView>(R.id.iv_selected_fileImg)
                val deleteImg = view.findViewById<ImageView>(R.id.iv_selected_deleteImg)
                val playVideo = view.findViewById<ImageView>(R.id.iv_selected_playVideo)
                val layoutParams = img.layoutParams as RelativeLayout.LayoutParams
                layoutParams.width = viewWight - 15
                layoutParams.height = viewWight - 15
                img.layoutParams = layoutParams
                val jobOptionObj = dataList[i]
                if (!CommonUtil.isVideoFile(jobOptionObj.localPath) || zwStrData == jobOptionObj.localPath) {
                    playVideo.visibility = View.GONE
                } else {
                    playVideo.visibility = View.VISIBLE
                }
                if (zwStrData == jobOptionObj.localPath) {
                    deleteImg.visibility = View.GONE
                    img.setOnClickListener({
                        getDeletedSelectedImageInter()?.continueSelectedFile(1)
                    })
                    LKImage.load().load("").scale(ScaleMode.CENTER_CROP)
                            .placeHolder(R.mipmap.icon_work_add)
                            .error(R.mipmap.icon_work_add).into(img)
                } else {
                    view.id = i
                    deleteImg.id = i + 10
                    deleteImg.setOnClickListener(deleteImageOnClick)
                    view.setOnClickListener(largerImageOnClick)
                    deleteImg.visibility = View.VISIBLE
                    LKImage.load().load(jobOptionObj.localPath).scale(ScaleMode.CENTER_CROP)
                            .placeHolder(R.mipmap.icon_def_image)
                            .error(R.mipmap.icon_def_image).into(img)
                }

                honLinearLayout.addView(view)
                if (i % 3 != 0) {
                    val tv = TextView(mContext)
                    val layoutParamstv = LayoutParams(LKScreenUtil.dp2px(3f), ViewGroup.LayoutParams.WRAP_CONTENT)
                    tv.layoutParams = layoutParamstv
                    honLinearLayout.addView(tv)
                }
            }
            llSelectedAll?.addView(honLinearLayout)
        }
    }


    /**
     * 清除view
     */
    private fun clearAllJobOptionView() {
        llSelectedAll?.removeAllViews()
    }

    /**
     * 删除文件事件的监听
     */
    private inner class DeleteImgageOnClick : OnClickListener {
        override fun onClick(v: View) {
            val position = v.id - 10
            getDeletedSelectedImageInter()?.deleteFile(position, 1)
        }
    }


    /**
     * 查看大图
     */
    private inner class LargerImageOnClick : OnClickListener {
        override fun onClick(v: View) {
            val position = v.id
            val path = dataList[position]
            if (TextUtils.isEmpty(path.localPath)) {
                return
            }
            getDeletedSelectedImageInter()?.lookLargerImage(position, 1)
        }
    }
}